//
//  ViewController.m
//  压缩与解压
//
//  Created by 中创 on 2019/11/28.
//  Copyright © 2019 LS. All rights reserved.
//

#import "ViewController.h"

#import "SSZipArchive.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:btn];
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"压缩" forState:UIControlStateNormal];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn addTarget:self action:@selector(createZip) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:btn1];
    btn1.backgroundColor = [UIColor redColor];
    [btn1 setTitle:@"解压" forState:UIControlStateNormal];
    btn1.frame = CGRectMake(100, 210, 100, 100);
    [btn1 addTarget:self action:@selector(unzip) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createZip{
    
    NSString * path = [[NSBundle mainBundle] pathForResource:@"0.头像" ofType:@"png"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString* zipPath = [documentPath stringByAppendingString:@"/images.zip"] ;
    NSArray * pathArr = @[path];
    // 对文件进行压缩
    [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:pathArr];
    // 对文件夹进行压缩
//    [SSZipArchive createZipFileAtPath:zipPath withContentsOfDirectory:@"" keepParentDirectory:YES];
}
- (void)unzip{
    NSString * path = [[NSBundle mainBundle] pathForResource:@"0.头像" ofType:@"zip"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString* unZipTo = [documentPath stringByAppendingString:@"/images"] ;
    [SSZipArchive unzipFileAtPath:path toDestination:unZipTo progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
        
    } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nonnull error) {
        NSLog(@"path:%@", path);
        NSLog(@"succeeded:%d", succeeded);
        NSLog(@"error:%@", error);
    }];
}


@end
